<?php

namespace App\Admin\Renderable;

use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;
use App\Models\WorkUser;
use Illuminate\Support\Facades\DB;

class TeamWorkUser extends LazyRenderable
{
    public function grid(): Grid
    {
        return Grid::make(new WorkUser(), function (Grid $grid) {
//            $grid->perPages([10, 20, 30, 40, 50,100,200,300,400,500]);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('engineering', '所属工程')->select(function ($status){
                    $Adminid  = Admin::user()->id;
                    if($Adminid != 1 && Admin::user()->whole != 0){
                        $engineering_name = DB::table('engineering')->where('manager','like','%'.$Adminid.'%')->where('deleted_at',null)->pluck('engineering_name');
                    }else{
                        $engineering_name = DB::table('engineering')->where('deleted_at',null)->pluck('engineering_name');
                    }
                    $arr = [];
                    foreach ($engineering_name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);
                $filter->like('id_card', '身份证号')->width(5);
                $filter->like('name', '姓名')->width(5);
                $filter->equal('mobile','手机号')->width(5);


                $filter->like('team', '所属队伍')->select(function ($team){
                    $name = DB::table('team')->where('deleted_at',null)->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

                $filter->equal('type_work','工种')->select(function ($team){
                    $name = DB::table('type_work')->pluck('name');
                    $arr = [];
                    foreach ($name as $key => $value){
                        $arr["$value"] = $value;
                    }
                    return $arr;
                })->width(5);

            });


            $grid->model()->where('status',1)->where('deleted_at',null)->where('team', $this->payload['team']);
            $grid->column('name','姓名')->display(function ($name) {
                $res = false;
                if (str_contains($this->photo_portrait, 'jpeg') || str_contains($this->photo_portrait, 'png')) {
                    $res = true;
                }
                if($res){
                    return $name;
                }else{
                    return "<span style='color:red'>$name</span>";
                }
            });
            $grid->column('engineering','所属工程');
            $grid->column('mobile','手机号');
            $grid->column('id_card','身份证号');

            $grid->column('photo_portrait','人像面')->image('','80','80');
            $grid->column('photo_emblem','国徽面')->image('','80','80');
//            $grid->column('bank_deposit','开户银行');
//            $grid->column('bank_number','银行卡号');
            $grid->disableActions();
//            $grid->column('enter_time','入职时间');
//            $grid->column('exit_time','离职时间');
//            $grid->quickSearch(['id', 'name', 'id_card']);
//            $grid->paginate(10);
//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->like('username')->width(4);
//                $filter->like('name')->width(4);
//            });

        });
    }
}
